<?php
/**
 * 作者:晨光
 * EMAIL:461718846@qq.com
 * DATE:2020/4/18
 */

namespace app\admin\controller;


use app\common\controller\AdminUserBase;
use app\common\controller\Excel;
use app\common\model\AdminLog;
use app\common\model\Dictionary;
use app\common\model\DictionaryItem;
use app\common\model\Options;
use think\Db;
use think\facade\Request;
use think\Validate;


class System extends AdminUserBase
{
    /**
     * 系统配置
     * @return \think\response\View
     */
    public function options(){
        $data = getOptions();
        $this->assign('data',$data);
        return view();
    }
    public function options_save(){
        try {
            $params = input('post.','','trim');
            $data = [];
            foreach ($params as $k=>$v){
                $data[$k] = [
                    'field'=>$k,
                    'value'=>$v,
                ];
            }
            $data = array_values($data);

            foreach ($data as $k=>$v){
                $system = Options::where(['field'=>$v['field']])->find();
                if (empty($system)){
                    $system = new Options();
                }
                $system->field = $v['field'];
                $system->value = $v['value'];
                $system->save();
            }

//            删除缓存
            delete_dir_file(env('runtime_path').'/temp/');

            saveAdminLog("编辑系统配置");
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }
    /**
     * 系统配置
     * @return \think\response\View
     */

    /**
     * 字典配置
     */
    public function dictionary(){
        if (Request::isAjax()){
            $params = input();
            $where = [];
            if (!empty($params['keywords'])){
                $where[] = ['name|code','like','%'.$params['keywords'].'%'];
            }
            $data = Dictionary::where($where)->select();
            $this->result($data);
        }
        return view();
    }
    public function dictionary_save(){
        $param = input();
        //        验证
        $validate = Validate::make([
            'name|名称'=>'require|unique:dictionary',
            'code|字典代码'=>'require|unique:dictionary|alphaNum',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }

        $model = new Dictionary();

        if (empty($param['id'])){
            $res = $model->allowField(true)->isUpdate(false)->save($param);
        }else{
            $res = $model->allowField(true)->isUpdate(true)->save($param,['id'=>$param['id']]);
        }

        if ($res !== false){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }
    public function dictionary_del(){
        $id = input('id');
//        检查是不是有字典项
        $isZhi = Db::name('DictionaryItem')->where(['dictionary_id'=>$id,'delete_time'=>0])->count();
        if ($isZhi > 0){
            $this->error('当前字典下有未删除的字典项，请先删除');
        }

        $res = Dictionary::destroy($id);
        if ($res !== false){
            saveAdminLog("删除字典配置");
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    /**
     * 字典配置
     */

    /**
     * 字典项配置
     */
    public function dictionary_item(){
        if (Request::isAjax()){
            $param = input();
            $where = [];
            if (!empty($param['dictionary_id'])){
                $where[] = ['dictionary_id','=', $param['dictionary_id']];
            }
            if (!empty($param['item'])){
                $where[] = ['name','like','%'.$param['item'].'%'];
            }
            $data = DictionaryItem::where($where)->order('sort desc')->paginate($param['limit']);

            $data = $data->toArray();
            $this->result($data['data'],$data['total'],'0',$param['dictionary_id']);
        }
        return view();
    }
    public function dictionary_item_save(){
        $param = input();

        $model = new DictionaryItem();

        if (empty($param['id'])){
            $res = $model->allowField(true)->isUpdate(false)->save($param);
            saveAdminLog("添加字典项");
        }else{
            $res = $model->allowField(true)->isUpdate(true)->save($param,['id'=>$param['id']]);
            saveAdminLog("编辑字典项");
        }

        if ($res !== false){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }
    public function dictionary_item_del(){
        $id = input('id');
        if (empty($id)){
            $id = input('ids');
        }
        $res = DictionaryItem::destroy($id);
        if ($res !== false){
            saveAdminLog("删除字典项");
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    /**
     * 字典项配置
     */

    /**
     * 后台日志
     */
    public function admin_log(){
        if (Request::isAjax()){
            $param = input();
            $data = AdminLog::getList($param);
            $this->result($data['data'],$data['total']);
        }
        return view('log');
    }
    /**
     * 后台日志
     */
}
